package com.atguigu.gmall.task;


import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.config.constant.MqCons;
import com.atguigu.gmall.common.config.entity.GmallCorrelationData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时器
 */
@EnableScheduling
@Slf4j
@Component
public class ScheduledTask {


    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 定时器
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void task(){

        String value = (String) redisTemplate.opsForList().rightPop(MqCons.MQ_KEY_PREFIX);
        System.out.println("定时器执行了:" + value);
        GmallCorrelationData gmallCorrelationData = JSON.parseObject(value, GmallCorrelationData.class);

        rabbitTemplate.convertAndSend(gmallCorrelationData.getExchange(),gmallCorrelationData.getRoutingKey()
        ,gmallCorrelationData.getMessage(),gmallCorrelationData);


    }

}
